/**
 * $Id: SpecialMobileMrg.java,v 1.21 2006/09/22 07:57:34 martin Exp $
 *
 * Copyright 2006 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.runner.specialmobile;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.haoxi.gateway.common.SQLConst;
import com.haoxi.gateway.common.SYSConst;
import com.haoxi.gateway.runner.RunnerMgr;
import com.haoxi.gateway.util.db.DBConnectUtil;

/**
 * 用于获得敏感号和
 * 
 * Revision History
 * 
 * 2006-5-30,star.Zhao,created it
 */
public class SpecialMobileMrg {
    private final static Log log = LogFactory.getLog(SpecialMobileMrg.class);
    public static HashMap senseMobileMap = new HashMap();
    public static HashMap blackMobileMap = new HashMap();
    public static HashMap blackLongNumMap = new HashMap();

    /**
     * 初始化函数
     * 
     * @return
     */
    public static boolean init() {
        try {
            // synchronize SenseMobile
            HashMap currentSenseMobileMap = SpecialMobileMrg
                    .getCurrentSenseMobileMap();
            if (null != currentSenseMobileMap) {
                synchronized (SpecialMobileMrg.senseMobileMap) {
                    SpecialMobileMrg.senseMobileMap.clear();
                    SpecialMobileMrg.senseMobileMap = currentSenseMobileMap;
                }
            }

            // synchronize black Mobile
            HashMap currentBlackMobileMap = SpecialMobileMrg
                    .getCurrentBlackMobileMap();
            if (null != currentBlackMobileMap) {
                synchronized (SpecialMobileMrg.blackMobileMap) {
                    SpecialMobileMrg.blackMobileMap.clear();
                    SpecialMobileMrg.blackMobileMap = currentBlackMobileMap;
                }
            }

            // synchronize black Mobile
            HashMap currentBlackLongNumMap = SpecialMobileMrg
                    .getCurrentBlackLongNumMap();
            if (null != currentBlackLongNumMap) {
                synchronized (SpecialMobileMrg.blackLongNumMap) {
                    SpecialMobileMrg.blackLongNumMap.clear();
                    SpecialMobileMrg.blackLongNumMap = currentBlackLongNumMap;
                }
            }
        } catch (Throwable e) {
            log.error("Exception in SpecialMobileMrg.init() ！", e);
            return false;
        }
        return true;
    }

    /**
     * 判断是否在黑名单中
     * 
     * @param mobile
     * @return
     */
    public static boolean isBlackMobile(String mobile) {
        return SpecialMobileMrg.blackMobileMap.containsKey(mobile);
    }

    /**
     * 判断是否敏感号
     * 
     * @param mobile
     * @return
     */
    public static boolean isSenseMobile(String mobile) {
        return SpecialMobileMrg.senseMobileMap.containsKey(mobile);
    }

    /**
     * 判断是需要屏蔽的长号码
     * 
     * @param mobile
     * @return
     */
    public static boolean isBlackLongNum(String longNum) {
        return SpecialMobileMrg.blackLongNumMap.containsKey(longNum);
    }

    /**
     * 设置敏感号到Map中
     */
    private static HashMap getCurrentSenseMobileMap() {
        HashMap result = new HashMap();

        Connection conn = null;
        Statement st = null;
        ResultSet rst = null;
        log.debug("---------start loading the sense mobile data!---------");
        String dbName = (String) RunnerMgr.getInstance().getSystemConf().get(
                SYSConst.CONF_DB_MYSQL);
        try {
            conn = DBConnectUtil.open(dbName);
            if (null != conn) {
                st = conn.createStatement();
                rst = st.executeQuery(SQLConst.SELECT_SENESEMOBILE);
                while (rst.next()) {
                    String mobile = rst.getString("mobile");
                    String detail = rst.getString("detail");
                    result.put(mobile, detail);
                }
                log
                        .debug("---------loading the sense mobile data successfully!---------");
            } else {
                log
                        .debug("---------SpecialMobileMrg  get DBConnect from DB connect pool failure!---------");
            }

        } catch (SQLException sqle) {
            log
                    .error(
                            "SQLException in SpecialMobileChg.getCurrentSenseMobileMap()!",
                            sqle);
            log
                    .debug("---------loading the sense mobile data failure!---------");
            throw new RuntimeException("SQLException");
        } finally {
            try {
                if (null != rst) {
                    rst.close();
                }
                if (null != st) {
                    st.close();
                }
                if (null != conn) {
                    conn.close();
                }
            } catch (SQLException sqle) {
                log
                        .error(
                                "release connection exception in SpecialMobileChg! error: ",
                                sqle);
                throw new RuntimeException("release connection Exception");
            }
        }

        // log.debug ( "currentSenseMobileMap length is: [" +
        // senseMobileMap.size () + "]" );
        return result;
    }

    /**
     * 设置黑名单中的号码到Map中
     */
    private static HashMap getCurrentBlackMobileMap() {
        HashMap result = new HashMap();

        Connection conn = null;
        Statement st = null;
        ResultSet rst = null;
        log.debug("---------start loading the black mobile data!---------");
        String dbName = (String) RunnerMgr.getInstance().getSystemConf().get(
                SYSConst.CONF_DB_MYSQL);
        try {
            conn = DBConnectUtil.open(dbName);
            st = conn.createStatement();
            rst = st.executeQuery(SQLConst.SELECT_BLACKMOBILE);
            while (rst.next()) {
                String mobile = rst.getString("mobile");
                String remark = rst.getString("remark");
                result.put(mobile, remark);
            }
            log
                    .debug("---------loading the black mobile data successfully!---------");
        } catch (SQLException sqle) {
            log.error("SQLException in SpecialMobileChg! error: ", sqle);
            log
                    .debug("---------loading the black mobile data failure!---------");
            throw new RuntimeException("SQLException in SpecialMobileChg!");
        } finally {
            try {
                if (null != rst) {
                    rst.close();
                }
                if (null != st) {
                    st.close();
                }
                if (null != conn) {
                    conn.close();
                }
            } catch (SQLException sqle) {
                log
                        .error(
                                "release connection exception in SpecialMobileChg! error：",
                                sqle);
                throw new RuntimeException(
                        "release connection exception in SpecialMobileChg!");
            }
        }

        // log.debug ( "blackMobileMap length is: [" + blackMobileMap.size () +
        // "]" );
        return result;
    }

    /**
     * 设置长号码黑名单中的长号码到Map中
     */
    private static HashMap getCurrentBlackLongNumMap() {
        HashMap result = new HashMap();

        Connection conn = null;
        Statement st = null;
        ResultSet rst = null;
        log.debug("---------start loading the black longnumber data!---------");
        String dbName = (String) RunnerMgr.getInstance().getSystemConf().get(
                SYSConst.CONF_DB_MYSQL);
        try {
            conn = DBConnectUtil.open(dbName);
            st = conn.createStatement();
            rst = st.executeQuery(SQLConst.SELECT_BLACKLONGNUM);
            while (rst.next()) {
                String mobile = rst.getString("longnumber");
                String remark = rst.getString("remark");
                result.put(mobile, remark);
            }
            log
                    .debug("---------loading the black longnumber data successfully!---------");
        } catch (SQLException sqle) {
            log.error("SQLException in SpecialMobileChg! error: ", sqle);
            log
                    .debug("---------loading the black longnumber data failure!---------");
            throw new RuntimeException("SQLException in SpecialMobileChg!");
        } finally {
            try {
                if (null != rst) {
                    rst.close();
                }
                if (null != st) {
                    st.close();
                }
                if (null != conn) {
                    conn.close();
                }
            } catch (SQLException sqle) {
                log
                        .error(
                                "release connection exception in SpecialMobileChg! error: ",
                                sqle);
                throw new RuntimeException(
                        "release connection exception in SpecialMobileChg");
            }
        }
        return result;
    }

}
